//
//  HSYScrollTextField.m
//  高仿暴走斗图
//
//  Created by key on 16/3/18.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYScrollTextField.h"
#import "UILabel+SuggestSize.h"

@interface HSYScrollTextField ()<UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITextField *textField;

@end
@implementation HSYScrollTextField

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}
- (void)setupForViewStorageWithTag:(NSInteger)tag userInteractionEnabled:(BOOL)userInteractionEnabled {
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    self.scrollView.clipsToBounds = YES;
    self.scrollView.backgroundColor = CLEAR_COLOR;
    [self addSubview:self.scrollView];
    
    self.textField = [[UITextField alloc] initWithFrame:self.scrollView.bounds];
    self.textField.tag = tag;
    self.textField.delegate = self;
    self.textField.textColor = [UIColor blackColor];
    self.textField.font = [UIFont systemFontOfSize:16.0];
    self.textField.textAlignment = NSTextAlignmentCenter;
    self.textField.layer.borderWidth = 0.0;
    self.textField.backgroundColor = [UIColor whiteColor];
    self.textField.userInteractionEnabled = userInteractionEnabled;
    
    [self.scrollView addSubview:self.textField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shouldChangeTextFieldNotification:) name:UITextFieldTextDidChangeNotification object:self.textField];
}


- (void)dealloc {
 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:self.textField];
}

#pragma mark - UITextFieldDelegate


- (void)shouldChangeTextFieldNotification:(NSNotification *)notification {
    
    UITextField *textField = notification.object;
    if (!textField || !self.shouldChangeText) {
        return;
    }
    self.shouldChangeText(textField, textField.text, self.mark);
    
    CGRect rect = self.frame;
    CGFloat width = [[UILabel new] suggestSizeForString:textField.text width:MAXFLOAT height:0].width;
    if (width > CGRectGetWidth(rect)) {
        
        self.scrollView.contentSize = CGSizeMake(width+5, 0);
        self.textField.viewWidth = self.scrollView.contentSize.width;
    } else {
        
        self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(rect), 0);
        self.textField.viewWidth = self.scrollView.contentSize.width;
    }
    
    self.scrollView.contentOffset = CGPointMake(self.scrollView.contentSize.width - CGRectGetWidth(self.bounds), 0);
}



- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (self.didEndEditing) {
        self.didEndEditing(textField, self.mark);
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    if (self.didBeginEditing) {
        self.didBeginEditing(textField, self.mark);
    }
}

@end
